package com.changgou.search.controller;


import com.changgou.entity.Page;
import com.changgou.search.pojo.Skuinfo;
import com.changgou.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

/**
 * @description:
 * @author: 大佬味的小男孩
 * @date: 2020-07-24 11:28
 **/

@Controller
@RequestMapping("search")
public class SearchController {
    @Autowired
    private SearchService searchService;

    /**
     * @param searchMap
     * @return java.util.Map
     * @description: 搜索服务
     * @author 大佬味的小男孩
     * @date 2020/07/26 23:39
     */
    @GetMapping
    @ResponseBody
    public Map search(@RequestParam Map<String, String> searchMap) {
        if (null != searchMap) {
            //对搜索入参带有特殊符号进行处理
            //获取map中每个键值对集合
            Set<Map.Entry<String, String>> entries = searchMap.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                //如果键值对中的key是以spec_开头
                if (entry.getKey().startsWith("spec_")) {
                    searchMap.put(entry.getKey(), entry.getValue().replace("+", "%2B"));
                }
            }
        }
        return searchService.search(searchMap);
    }


    /**
     * @param searchMap
     * @param model
     * @return java.lang.String
     * @description: 搜索页面渲染
     * @author 大佬味的小男孩
     * @date 2020/07/27 14:47
     */
    @GetMapping("list")
    public String list(@RequestParam Map<String, String> searchMap, Model model) {
        //对搜索入参带有特殊符号进行处理
        if (null != searchMap) {
            Set<Map.Entry<String, String>> entries = searchMap.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                if (entry.getKey().startsWith("spec_")) {
                    searchMap.put(entry.getKey(), entry.getValue().replace("+", "%2B"));
                }
            }
        }
        Set<String> keySet = searchMap.keySet();
        for (String s : keySet) {
            System.out.println(s + " ："+searchMap.get(s));
        }
        Map resultMap = searchService.search(searchMap);


        StringBuilder url = new StringBuilder("/search/list");
        //拼接url
        if (searchMap != null && searchMap.size() > 0) {
            url.append("?");
            for (String paramKey : searchMap.keySet()) {
                if (!"sortRule".equals(paramKey) && !"sortField".equals(paramKey) && !"PageNum".equals(paramKey)) {
                    url.append(paramKey).append("=").append(searchMap.get(paramKey)).append("&");
                }
            }
            String urlString = url.toString();
            //去掉最后一个&
            urlString = urlString.substring(0, urlString.length() - 1);
            model.addAttribute("url", urlString);
        } else {
            model.addAttribute("url", url);
        }

        //设置分页查询数据
        Page<Skuinfo> page = new Page<Skuinfo>(
                Long.parseLong(String.valueOf(resultMap.get("total"))),
                Integer.parseInt(String.valueOf(resultMap.get("pageNum"))),
                Page.pageSize
        );


    //设置视图数据
        //返回数据
        model.addAttribute("resultMap", resultMap);
        //查询条件数据
        model.addAttribute("searchMap", searchMap);
        //分页数据
        model.addAttribute("page", page);


        return "search";
    }


}

